//
//  QKWelcomeViewController.m
//  QKWeibo-OC
//
//  Created by iMac_Kurt on 16/8/25.
//  Copyright © 2016年 Kurt_Qi. All rights reserved.
//

#import "QKWelcomeViewController.h"
#import "Masonry.h"
#import "UILabel+QKLabel.h"
#import "QKUserAccountViewModel.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface QKWelcomeViewController ()

@property (nonatomic, strong) UIImageView *iconView;

@property (nonatomic, strong) UILabel *welcomeLabel;

@end

@implementation QKWelcomeViewController

// 视图加载完成：设置数据
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 更换头像图片（网络获取图片使用SDWebImage）
    [self.iconView sd_setImageWithURL:[QKUserAccountViewModel sharedUserAccount].avatarURL placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
}

// 加载视图：设置界面
- (void)loadView {
    // 切换根视图为imageView作为背景图像
    self.view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
    
    // 设置界面
    [self setupUI];
}

// 视图已经显示：执行动画或处理键盘
- (void)viewDidAppear:(BOOL)animated {
    [self startAnimation];
}

- (void)startAnimation {
    // iconView目标位置
    [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(200 - self.view.bounds.size.height);
    }];
    
    // 动画
    self.welcomeLabel.alpha = 0;
    [UIView animateWithDuration:1.2 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:6 options:UIViewAnimationOptionLayoutSubviews animations:^{
        // 强制更新布局：注意是self.view调用layoutIfNeeded
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1.0 animations:^{
            self.welcomeLabel.alpha = 1.0;
        } completion:^(BOOL finished) {
            NSLog(@"动画完成");
            
            // 切换根控制器
            [[NSNotificationCenter defaultCenter] postNotificationName:QKSwitchRootViewControllerNotification object:nil];
            
        }];
    }];
}

- (void)setupUI {
    // 添加子控件
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.welcomeLabel];
    
    // 自动布局
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        // 位置
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-200);
        // 大小
        make.width.equalTo(@90);
        make.height.equalTo(@90);
    }];
    [self.welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_centerX);
        make.top.equalTo(self.iconView.mas_bottom).offset(15);
    }];
}

#pragma mark - 懒加载控件
- (UIImageView *)iconView {
    if (_iconView == nil) {
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
        
        // 切圆
        iv.layer.cornerRadius = 45;
        iv.layer.masksToBounds = YES;
        
        _iconView = iv;
    }
    return _iconView;
}
- (UILabel *)welcomeLabel {
    if (_welcomeLabel == nil) {
        UILabel *label = [UILabel labelWithText:@"欢迎归来" color:[UIColor darkGrayColor] fontSize:14];
        
        _welcomeLabel = label;
    }
    return _welcomeLabel;
}

@end
